package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.domain.Cart;
import JPetStore.domain.LineItem;
import JPetStore.domain.Order;
import JPetStore.persistence.CartDao;
import JPetStore.persistence.LineItemDao;
import JPetStore.persistence.OrderDao;
import JPetStore.persistence.impl.CartDaoImpl;
import JPetStore.persistence.impl.LineItemDaoImpl;
import JPetStore.persistence.impl.OrderDaoImpl;
import JPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;

public class NewOrderFormServlet extends HttpServlet {

    private OrderService orderService;

    private static final String NEW_ORDER_FORM = "/WEB-INF/jsp/order/newOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if(loginAccount == null) {
            resp.sendRedirect("signOnForm");
        }else{
            try{
                CartDao cartDao = new CartDaoImpl();
                cartDao.deleteAll(loginAccount);
            }catch (Exception e){
                e.printStackTrace();
            }
            req.getRequestDispatcher(NEW_ORDER_FORM).forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            String cardtype = req.getParameter("card_type");
            String creitcard = req.getParameter("card_number");
            String exprdate = req.getParameter("expiry_date");
            String shiptofirstname = req.getParameter("first_name");
            String shiptolastname = req.getParameter("last_name");
            String shipaddr1= req.getParameter("address1");
            String shipaddr2= req.getParameter("address2");
            String shipcity = req.getParameter("city");
            String shipstate = req.getParameter("state");
            String shipzip = req.getParameter("zip");
            String shipcountry = req.getParameter("country");
            //int ship = Integer.parseInt(req.getParameter("ship"));
            HttpSession httpSession = req.getSession();
            Account account = (Account) httpSession.getAttribute("loginAccount");
            this.orderService = new OrderService();
            int orderid = orderService.getLatestOrderId() + 1;
            Cart cart = (Cart) httpSession.getAttribute("cart");
            if(cart == null){
                System.out.println("cart is null");
            }
            else {
                BigDecimal totalprice = cart.getSubTotal();
            Order order = new Order();
                // 获取当前的日期
                LocalDate localDate = LocalDate.now();
                // 将LocalDate转换为java.sql.Date
                Date orderdate = Date.valueOf(localDate);
            order.init(orderid,shipaddr1,shipaddr2,shipcity,shipstate,shipzip,shipcountry,totalprice,shiptofirstname,shiptolastname,creitcard,exprdate,cardtype,orderdate);
            httpSession.setAttribute("order",order);
            this.orderService.insertOrder(account,order);

            LineItemDao lineItemDao = new LineItemDaoImpl();
            for(int i = 0;i < cart.getCartItemList().size();i++){
                LineItem lineItem = new LineItem(i+1,cart.getCartItemList().get(i),orderid);
                lineItemDao.insertLineItem(lineItem);
            }
            }
            resp.sendRedirect("confirmOrderForm");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
